package git.determinal.template.view;





import cn.hutool.core.util.TypeUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClassUtil implements Serializable {
    private static final long serialVersionUID = 6398742654189791218L;
    private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);

    public ClassUtil() {
    }

    public static long getSerialVersionUID(Class<?> type) {
        try {
            Field field = type.getDeclaredField("serialVersionUID");
            if (field != null) {
                field.setAccessible(true);
                return field.getLong(type);
            }
        } catch (Throwable var2) {
            logger.error("get SerialVersionUID fail:", var2);
        }

        return 0L;
    }

    public static ClassLoader getClassLoader() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = ClassUtil.class.getClassLoader();
        }

        return loader;
    }

    public static Class<?> getClassByName(String className) throws ClassNotFoundException {
        return getClassLoader().loadClass(className);
    }

    public static String formatClassName(String className) {
        if (className.startsWith("L") && className.endsWith(";")) {
            className = className.substring(1, className.length() - 1);
        }

        className = className.replace('/', '.');
        return className;
    }

    public static Class<?> forName(String className) throws ClassNotFoundException {
        return getClassLoader().loadClass(formatClassName(className));
    }

    public static URL getURL(String path) {
        ClassLoader loader = getClassLoader();
        return loader.getResource(path);
    }

    public static Class getFieldClass(Class clazz, String fieldName) throws NoSuchFieldException {
        Field field = clazz.getDeclaredField(fieldName);
        if (field != null) {
            return field.getType();
        } else {
            Class parentClazz = clazz.getSuperclass();
            return parentClazz != null ? getFieldClass(parentClazz, fieldName) : null;
        }
    }

    public static InputStream getInputStreamInClass(String path, String extName) {
        ClassLoader loader = getClassLoader();
        StringBuffer buf = (new StringBuffer(path.replace('.', '/'))).append(extName);
        return loader.getResourceAsStream(buf.toString());
    }

    public static InputStream getInputStreamInClass(String path) {
        ClassLoader loader = getClassLoader();
        return loader.getResourceAsStream(path);
    }

    public static URL getResourceInClass(String path) {
        ClassLoader loader = getClassLoader();
        return loader.getResource(path);
    }

    public static Enumeration<URL> getResourcesInClass(String path) throws IOException {
        ClassLoader loader = getClassLoader();
        return loader.getResources(path);
    }

    public static Object getInstance(String className) throws Exception {
        Class<?> clz = getClassByName(className);
        return clz.newInstance();
    }

    public static Object getInstance(String className, Object[] args) throws Exception {
        Class<?> clz = getClassByName(className);
        if (args == null) {
            return clz.newInstance();
        } else {
            Class<?>[] argsClz = new Class[args.length];

            for(int i = 0; i < args.length; ++i) {
                argsClz[i] = args[i].getClass();
            }

            Constructor<?> ctor = clz.getConstructor(argsClz);
            return ctor.newInstance(args);
        }
    }

    public static Class<?> loadClass(String name) throws ClassNotFoundException {
        return getClassLoader().loadClass(name);
    }

    public static Properties loadProperties(String classPath) {
        Properties prop = new Properties();

        try {
            Enumeration urls = getClassLoader().getResources(classPath);

            while(urls.hasMoreElements()) {
                URL url = (URL)urls.nextElement();

                try {
                    prop.load(url.openStream());
                } catch (Throwable var5) {
                    logger.warn("Load Config Fail:" + url.toExternalForm(), var5);
                }
            }
        } catch (IOException var6) {
            logger.warn("Load Config Fail", var6);
        }

        return prop;
    }

    public static List<String> loadConfigs(String classPath) {
        ArrayList list = new ArrayList();

        try {
            Enumeration urls = getClassLoader().getResources(classPath);

            while(urls.hasMoreElements()) {
                URL url = (URL)urls.nextElement();

                try {
                    LineNumberReader line = new LineNumberReader(new InputStreamReader(url.openStream()));
                    String key = null;

                    while((key = line.readLine()) != null) {
                        if (key.trim().length() > 0) {
                            String name = key.trim();
                            if (!name.startsWith("#")) {
                                int n = name.indexOf(61);
                                if (n != -1) {
                                    name = name.substring(0, n).trim();
                                }

                                if (name.length() > 0) {
                                    list.add(name);
                                }
                            }
                        }
                    }
                } catch (Throwable var8) {
                    logger.warn("Load Config Fail:" + url.toExternalForm(), var8);
                }
            }
        } catch (IOException var9) {
            logger.warn("Load Config Fail", var9);
        }

        return list;
    }

    public static Class<?> getObjectType(Object object) {
        Class<?> clz = object.getClass();
        return clz.isArray() ? clz.getComponentType() : clz;
    }

    public static Enumeration<URL> getResources(String classPath) throws IOException {
        return getClassLoader().getResources(classPath);
    }

    public static Class<?> getTypeArgument(Class<?> clazz) {
        return getTypeArgument(clazz, 0);
    }

    public static Class<?> getTypeArgument(Class<?> clazz, int index) {
        Type argumentType = TypeUtil.getTypeArgument(clazz, index);
        return TypeUtil.getClass(argumentType);
    }
}

